So today we're going to take a look at Raycasting.
Now I put Raycasting after pathfinding as those the two both.
So because Raycasting does have some applications with pathfinding basically um with if you want to
make a um, a very good like NPC movement kind of thing, um, you can use raycasting and pathfinding
is like a backup almost.
Um, so yeah.
Um, but basically, well, what is raycast raycasting to start off with?
Well, Raycasting is basically casting an invisible ray from one position into the direction or another
position.
And in this way you can basically detect if any parts intersect that ray at any point.
So basically you can check if there is clear line of sight between two different objects.
And this is incredibly useful.
Um, perhaps you've got, for example, instead of using pathfinding 24 seven if there if the if the
NPC has clear view of the character, it should just be able to walk straight in order to get to it.
Um, Raycasting is also really cool for like lasers as well.
As soon as the player goes through, um, these invisible rays, they can be detected and lots of other
stuff as well.
So let's add a script into server script service and have a go at how to do ray casting.
So let's add in a part.
And as we were it'll originate from.
So our ray is going to originate from this part.
Um, so let's reference it.
So let's say local parts is equal to game dot workspace dot parts like that there.
Okay.
Um we then want to create array.
And funnily enough we're not going to use um ray service okay.
There's no such thing as ray service.
We don't use that to, to create arrays.
So um, yeah, we instead use a ray function so local let's call it my ray because ray is unfortunately
taken.
So you have to.
His main difference is my ray is equal to ray dot new like this here.
And we can put in here the origin, which is where this invisible ray starts.
And then the direction which it occurs, which is kind of fired into.
Now a direction does not mean the end point of the ray.
It will go in the direction of that position, but it, it won't finish it if it passes it, and it
won't keep going until it gets to it, it will just go in the direction of it.
So we're going to go into the parked position there and let's put it into um, let's add in a second
part, perhaps over here.
Um.
Part one.
And put it in that position.
So local part one is equal to game workspace part one.
And then let's get it there.
So um part one dot position here as well.
So it's going to originate from part part from the first part.
And go in the direction of part one there as well okay.
And we've now created array.
It's that simple.
Just that one line of code.
And allowed us to create a way between these two parts.
I'm going to make this one here in not invisible, but a lower transparency just so we can know where
it's going.
It's going from this part over to here.
Um, but obviously we want to do some stuff.
Um, if we detect a collision, that's the biggest use of array is per collision is for collisions.
So, um, we are basically going to check to see if there is a collision on the array.
Now we want to check a lot of times we don't we don't want to check once.
Because if we just check if we just check once.
Um.
Is going to constantly update.
So if you're wanting to detect, for example, when my player walks, it walks into this array.
Um, we want to check it multiple times so it updates.
So we're going to use a while true do loop want these loops in a while but while true do loop.
Um basically constantly repeat this code let's say every one second like that.
Fair.
So we are going to um have a look at this.
So we are going to detect to see if there were any collisions.
Now we do this by assigning this function.
The function we're going to use two variables one one hit.
One position hit will be the object that collided with it.
And position would be the position at which the collision occurred.
So you need to make two variables to assign to this function is it outputs two values.
Basically game dot workspace colon finds parts on ray.
Um.
And they've changed this.
It used to be whitelist and blacklist.
It's now whitelist and ignore list.
And you can also do it just normally find part on RTÉ.
The difference between these three functions.
Find part on RTÉ.
Find part on RTÉ on RTÉ with ignore list and find Part on RTÉ with white list is to do of the parts
that it's the which part, which part is it accepts, so find part on RTÉ.
It will accept any part just normally and ignore list.
It allows you to um give it objects which if it detects, if it detects if they have collided with the
RTÉ, it will ignore them.
So this is good for we probably want to ignore.
Well this part here is the RTÉ starts from a center.
It's going to pass through a bit of his part before it exits it, so that'll be one of the ones we want
to put on our ignore list.
Um, and then the white list basically means that, um, you can label some parts that are the only
ones it will detect.
So you can put down free parts on a white list, and it will only detect if those free parts collide
with array.
So there's lots of, um, customizations you can do with this.
Here I'm going to use part part on array with ignore list.
And I'm going to use that there.
So the array is my array here.
And we ignore descendants table I'm just going to write down part and part one there.
Two.
There are two objects I'm going to ignore.
The mom.
Also some other options.
So if I can scroll on this, here we go.
So, um, terrain cells are cubes and ignore water.
Basically.
Do you want to treat, um, terrain as intersecting the ray?
Again, this isn't really needed for what we're doing at the moment.
I'm going to say false.
And should you ignore water, let's just say false as well.
Okay.
Now we've got this.
We want to check to see if there is a collision and if there's a collision hit is going to have some
kind of value because it's going to return the object which collided.
So if hit then there's an intersection.
Let's just print intersection out of.
So intersection.
Here.
Um, and, um, yeah, that's pretty much it.
So raycasting isn't too difficult.
It's not too much code involved with it, but it does have lots of applications.
So, um, let's just quickly run the game and see if it will detect my player.
So I need to play the game and see if it if it can detect my player going through it through these two
objects.
If I go through here.
Only taken once.
It's only checking once.
Um.
Every second.
But, um, can we see it printing any points?
Not quite.
Um, it might not detect it properly.
Let's change it around a bit.
Let's also, um, print.
Else print.
No intersection.
And let's do it every ten times a second and move these two objects up a bit.
Like that there.
Okay, let's try this again.
When it detects me.
There we go.
It can print.
It's printing their intersections like this here.
Why?
It's going in and out as if it's an intersection or not intersection.
Because once it does it, once, you have to collide with it again in order for it to work.
So you can see it's a nice intersection there as well.
So that's pretty much it really.
There's not really much else I can really mention I think is really worth mentioning about Raycasting.
Um, it has lots of applications.
Some of the ones, um, are to do with finding if it has line of sight, which is good for NPCs.
Um, other thing, other ones I could think of, um, something as simple as like a door opening, um,
instead of doing it instead of opening.
So an automatic door, which opens when the player gets near.
You could do it if it has line of sight of the door.
Um, within a certain distance, instead of it being.
Instead of it only being within a certain distance, it has to be within a certain distance and to be
able to see the door so you can actually see it opening.
Um.
Some other cases as well.
Perhaps if there's like a horror game, for example, and if the monster ever sees you, you then you
then die just then.
Um, that could be a cool application of raycasting as well.
So there are lots of different ways that you can use Raycasting.
Also, um, I can't talk about, um, I can't talk about applications of Raycasting without talking
about weapons.
Okay.
Um, guns, crossbows, bows, anything, any kind of, um, bullet based weapon involves raycasting.
Okay.
Um, when you fire a bullet, um, you will cast a ray.
And if the if any of it intersects the ray.
So where the bullet would travel, if any damage or kill, if any player intersects with that ray.
So Ray casting is is basically ray casting is a bit of code which you wouldn't know.
Um, in order for you to make a weapon, I'm just trying to think if there's anything else to do with
a gun, but you don't necessarily know about, um.
Mm.
Not really.
There's a bit of, um, it depends if what type of weapon you're wanting to make.
I mean, um, the simple weapons.
Absolutely not.
For more complex weapons, you might need to do a bit of positioning to do of the mouse and stuff.
Um, but that's pretty much it.
Um, so, yeah, lots of applications, and it's really worth just checking out.
Really.
So hopefully now you've got a good understanding of ATS and you're able to try out some of those applications.
If you want to have a go at making a simple a simple weapon, absolutely go for it.
As um, a lot of, a lot of shooter games um, will obviously require bow.
So they are very useful.
And the last, that one.
And in the next lecture we're going to have a look very quick one about OS time what that is.
So join me in the next lesson.
In the next lecture where we're going to have a look at that.